package com.tjetc.view;

import com.tjetc.domain.Animal;
import com.tjetc.domain.Category;
import com.tjetc.domain.ParkAnimal;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AnimalService;
import com.tjetc.service.ParkAnimalService;
import com.tjetc.service.ParkService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class ParkAnimalView {
    private static Scanner sc = new Scanner(System.in);
    private AnimalService animalService = ClientProxy.getProxyInstance(AnimalService.class);
    private ParkAnimalService parkAnimalService=ClientProxy.getProxyInstance(ParkAnimalService.class);

    public void start() {
        while (true) {
            System.out.println("1.分配园区给宠物");
            System.out.println("2.重新分配园区");
            System.out.println("0.返回上一个界面");
            System.out.println("请输入你的选择(0~2)");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    fenPei();
                    break;
                case 2:
                    reFenPei();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("请重新输入");
            }
        }
    }

    private void reFenPei() {
        list(0);
        System.out.println("请选择宠物id");
        int id= sc.nextInt();
        ParkManageView parkManageView=new ParkManageView();
        if (parkManageView.list()) {
            System.out.println("请输入园区id");
            int id1= sc.nextInt();
            ParkAnimal parkAnimal = new ParkAnimal();
            parkAnimal.setAnimalId(id);
            parkAnimal.setParkId(id1);
            Result result=parkAnimalService.update(parkAnimal);
            System.out.println(result.getMsg());
        }
    }

    private void fenPei() {
        list(1);
        System.out.println("请选择宠物id");
        int id= sc.nextInt();
        ParkManageView parkManageView=new ParkManageView();
        if (parkManageView.list()) {
            System.out.println("请输入园区id");
            int id1= sc.nextInt();
            ParkAnimal parkAnimal = new ParkAnimal();
            parkAnimal.setAnimalId(id);
            parkAnimal.setParkId(id1);
            Result result=parkAnimalService.insert(parkAnimal);
            System.out.println(result.getMsg());
        }

    }

    /**
     * status为1，则选择没有分配的，0则选择所有
     *
     * @param status
     */
    private void list(int status) {
        //获取
        Result result = animalService.listByStatus(2);
        List<Animal> list = (List<Animal>) result.getData();
        if (list.size() > 0) {
            for (Animal animal : list) {
                if (status == 0) {
                    if (animal.getPark() != null) {
                        System.out.print("id"+animal.getId()+",种类"+animal.getCid()+",品种:" + animal.getBreed() + ",年龄" + animal.getAge() + ",状态:" + animal.getStatus());
                        System.out.println("正处于" + animal.getPark().getDescription() + "园区");
                    }

                }
                if (status == 1) {
                    if (animal.getPark() == null) {
                        System.out.println("id:"+animal.getId()+",种类:"+animal.getCid()+",品种:" + animal.getBreed() + ",年龄" + animal.getAge() + ",状态:" + animal.getStatus());
                    }
                }
            }
        } else {
            System.out.println("没人啊");
        }

    }
}
